/**
 * 
 */
package com.airlines.base.service.dict.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.base.entity.dict.DictionaryView;
import com.airlines.base.entity.dict.MasterData;
import com.airlines.base.service.dict.DictionaryService;
import com.airlines.base.service.dict.MasterDataService;
import com.airlines.common.MultiselectUtil;
import com.airlines.common.TraceUtil;

import cn.mercury.utils.JsonUtils;

/**
 * @author xxxx
 * @date 2017年8月22日
 * @company xxxx Airlines
 * @description
 */
@Service
public class MasterDataServiceImpl implements MasterDataService {
	private static final Logger logger = LoggerFactory.getLogger(MasterDataServiceImpl.class);

//	@Autowired
//	private MasterDataMapper masterDataMapper;
	@Autowired
	private DictionaryService dictionaryService;

	@Override
	public <T> List<MasterData<T>> listMasterDataByCode(String code) {
		return null;
	}

	@Override
	public <T> List<T> convert(List<T> list, String idColumnName,String valueColumnName, String masterDataCode) {
		if (list != null && list.size() > 0) {
			List<MasterData<T>> masterDataList = this.listMasterDataByCode(masterDataCode);
			for (T t : list) {
				String id = null;
				try {
					id = BeanUtils.getProperty(t, idColumnName);
					if (StringUtils.isEmpty(id)) {
						continue;
					}
					String value = this.getValueByIdFromMasterDataList(id,masterDataList);
					BeanUtils.setProperty(t, valueColumnName, value);
				} catch (Exception e) {
					TraceUtil.error(logger, e);
				}
			}

		}
		return list;
	}

	@Override
	public <T> List<MasterData<T>> convertDictionaryViewListToMasterDataList(List<DictionaryView> list) {
		List<MasterData<T>> masterDataList = new ArrayList<>();
		for(DictionaryView  dv : list){
			MasterData<T> md = new MasterData<T>();
			md.setId(dv.getId());
			md.setValue(dv.getText());
			masterDataList.add(md);
		}
		return masterDataList;
	}

	@Override
	public <T> String getValueByIdFromMasterDataList(String id,
			List<MasterData<T>> list) {
		String value = null;
		int textCount = 0;
		for (MasterData<T> masterData : list) {
			if (StringUtils.equals(id, masterData.getId())) {
				value = masterData.getValue();
				textCount++;
			}
		}
		if (textCount == 1) {
			return value;
		} else {
			return id;
		}
	}



	@Override
	public <T> List<T> convertFromDictCode(List<T> list, String idColumnName,String valueColumnName, String dictCode) {
		List<DictionaryView> dictList = dictionaryService.listDictionaryView(dictCode);
		List<MasterData<T>> masterDataList = this.convertDictionaryViewListToMasterDataList(dictList);
		
		for(T t : list) {
			String id = null;
			try {
				id = BeanUtils.getProperty(t, idColumnName);
				if (StringUtils.isEmpty(id)) {
					continue;
				}
				String value = this.getValueByIdFromMasterDataList(id,masterDataList);
				BeanUtils.setProperty(t, valueColumnName, value);
			} catch (Exception e) {
				TraceUtil.error(logger, e);
			}
		}
		
		return list;
	}


	@Override
	public <T> String getMultiselectDataByMasterDataList(List<MasterData<T>> listMasterData, Integer defaultIndex) {
		Map<String, String> map = new HashMap<String,String>();	
		List<DictionaryView> list =  new ArrayList<>();
		for(MasterData<T> m : listMasterData){
			DictionaryView d = new DictionaryView();
			d.setId(m.getId());
			d.setText(m.getValue());
			list.add(d);
		}
		//默认值处理
		String defaultVal = null;
		if(defaultIndex !=null && defaultIndex < listMasterData.size()){
			defaultVal = list.get(defaultIndex).getId();
		}
		if(list !=null && list.size()==1){
			//如果下拉框只有一个选项，则该选择默认选中
			defaultVal = list.get(0).getId();
		}
		map.put("jsonData", MultiselectUtil.toJosn(list,defaultVal, "id", "text",null));
		map.put("defaultVal", defaultVal);
		return JsonUtils.toJson(map);
	}

	@Override
	public <T> String getMultiselectDataByMasterDataList(String code,Integer defaultIndex) {
		List<MasterData<T>> list = this.listMasterDataByCode(code);
		return getMultiselectDataByMasterDataList(list,defaultIndex);
	}
}
